import gettext
from flask import Flask, request, jsonify

app = Flask(__name__)

# 设置翻译目录
locales = {
    'en': gettext.translation('messages', localedir='locale', languages=['en']),
    'zh': gettext.translation('messages', localedir='locale', languages=['zh'])
}

# 设置默认语言
default_locale = 'en'

@app.route('/content', methods=['GET'])
def get_content():
    # 获取请求中的语言参数
    lang = request.args.get('lang', default_locale)
    
    if lang not in locales:
        lang = default_locale
    
    # 激活对应的翻译
    translation = locales[lang]
    translation.install()
    
    # 获取翻译文本
    content_type = request.args.get('type', 'greeting')
    if content_type == 'greeting':
        text = _("Hello, World!")
    elif content_type == 'farewell':
        text = _("Goodbye!")
    else:
        return jsonify({"error": "Invalid content type"}), 400
    
    return jsonify({content_type: text})

if __name__ == '__main__':
    app.run(debug=True)
